/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.cycle.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.CycleTimeException;
import com.pitop.cycle.api.CycleTimeFeignApi;
import com.pitop.cycle.entity.CycleTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 生产周期
 *
 * @author berton
 */
@Service("cycleTimeSVImpl")
@Slf4j
public class CycleTimeSVImpl {

    @Autowired
    private CycleTimeFeignApi cycleTimeFeignApi;

    /**
     * 加载一个对象CycleTime
     *
     * @param id id* @param code 编码
     * @return CycleTime
     */
    public CycleTime load(Long id, String code) {
        if (id == null && code == null) {
            throw new CycleTimeException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return cycleTimeFeignApi.load(id, code);
    }

    /**
     * 加载一个对象CycleTime 通过id
     *
     * @param id id
     * @return CycleTime
     */
    public CycleTime loadById(Long id) {
        if (id == null) {
            throw new CycleTimeException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return cycleTimeFeignApi.loadById(id);
    }

    /**
     * 加载一个对象CycleTime 通过code
     *
     * @param code 编码
     * @return CycleTime
     */
    public CycleTime loadByCode(String code) {
        if (code == null) {
            throw new CycleTimeException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return cycleTimeFeignApi.loadByCode(code);
    }


    /**
     * 删除对象CycleTime
     *
     * @param id id* @param code 编码
     * @return CycleTime
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new CycleTimeException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        cycleTimeFeignApi.delete(id, code);
    }


    /**
     * 查询CycleTime分页
     *
     * @param cycleTime 对象
     * @param curPage   当前页
     * @param pageSize  分页条数
     * @return List<CycleTime>
     */
    public List<CycleTime> list(CycleTime cycleTime, int curPage, int pageSize) {
        return cycleTimeFeignApi.list(cycleTime, curPage, pageSize);
    }

    /**
     * 查询CycleTime分页
     *
     * @param id       id* @param code  编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<CycleTime>
     */
    public List<CycleTime> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        CycleTime cycleTime = new CycleTime();
        cycleTime.setId(id);
        cycleTime.setCode(code);

        return cycleTimeFeignApi.list(cycleTime, curPage, pageSize);
    }


    public int count(Long id, String code) {
        return cycleTimeFeignApi.count(id, code);
    }


    public int count(CycleTime cycleTime) {
        return cycleTimeFeignApi.count(cycleTime);
    }


    /**
     * 保存
     * 1.验证数据合法性
     * ****数据非空验证
     * ****生产周期是否已经被使用
     * 2.保存生产周期
     *
     * @param cycleTime 实体
     * @throws BaseException
     */
    public void save(CycleTime cycleTime) {
        //1.验证数据合法性
        if (cycleTime == null) {
            throw new CycleTimeException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //生产周期是否被使用
        CycleTime cycleTimeFlag = cycleTimeFeignApi.loadByCycle(cycleTime.getCycle());
        if (cycleTimeFlag != null) {
            throw new CycleTimeException(BaseException.BaseExceptionEnum.Exists);
        }
        //保存生产周期
        cycleTimeFeignApi.build(cycleTime);
    }

    /**
     * 更新
     * 1.验证数据合法性
     * ****数据非空验证
     * ****生产周期是否已经被使用
     * 2.更新生产周期
     *
     * @param cycleTime 实体
     * @throws BaseException
     */
    public void modify(CycleTime cycleTime) {
        //1.验证数据合法性
        if (cycleTime == null) {
            throw new CycleTimeException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //生产周期是否被使用
        CycleTime cycleTimeFlag = cycleTimeFeignApi.loadByCycle(cycleTime.getCycle());
        if (cycleTimeFlag != null) {
            throw new CycleTimeException(BaseException.BaseExceptionEnum.Exists);
        }
        //2.更新生产周期
        cycleTimeFeignApi.modify(cycleTime);
    }

}
